package io.renren.modules.sys.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.renren.modules.sys.dao.QAnswererDao;
import io.renren.modules.sys.dao.QInvestigationDao;
import io.renren.modules.sys.dao.QQuestionDao;
import io.renren.modules.sys.entity.QAnswerer;
import io.renren.modules.sys.entity.QQuestion;
import io.renren.modules.sys.entity.QQuestionnaires;
import io.renren.modules.sys.service.QAnswererService;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * (QAnswerer)表服务实现类
 *
 * @author makejava
 * @since 2024-07-03 16:24:19
 */
@Service("qAnswererService")
public class QAnswererServiceImpl implements QAnswererService {
    @Resource
    private QAnswererDao qAnswererDao;
    @Resource
    private QQuestionDao qQuestionDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public QAnswerer queryById(String id) {
        return this.qAnswererDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param qAnswerer 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<QAnswerer> queryByPage(QAnswerer qAnswerer, PageRequest pageRequest) {
        long total = this.qAnswererDao.count(qAnswerer);
        return new PageImpl<>(this.qAnswererDao.queryAllByLimit(qAnswerer, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param qAnswerer 实例对象
     * @return 实例对象
     */
    @Override
    public QAnswerer insert(QAnswerer qAnswerer) {
        this.qAnswererDao.insert(qAnswerer);
        return qAnswerer;
    }

    /**
     * 修改数据
     *
     * @param qAnswerer 实例对象
     * @return 实例对象
     */
    @Override
    public QAnswerer update(QAnswerer qAnswerer) {
        this.qAnswererDao.update(qAnswerer);
        return this.queryById(qAnswerer.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.qAnswererDao.deleteById(id) > 0;
    }

    public List<QQuestion> getQuestionsByAnswerersId(String investigationId) throws JsonProcessingException
    {
        List<QQuestion> questions = new ArrayList<>();
        QQuestionnaires qQuestionnaires = qAnswererDao.getQuestionIdsByAnswererId(investigationId);
        if (qQuestionnaires != null) {
            ObjectMapper mapper = new ObjectMapper();
            String questionsIdsStr = qQuestionnaires.getQuestions();
            String[] questionsIdsArray = mapper.readValue(questionsIdsStr, String[].class);
            questions = qQuestionDao.queryByIds(Arrays.asList(questionsIdsArray), String.join(",", questionsIdsArray));
        }
        return questions;
    }
}
